package com.cmtime.future;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class FutureDemo {

	public static void main(String[] args) {
		testFuture();
	}
	
	public static void testFuture(){
		ExecutorService service = Executors.newCachedThreadPool();
		Future<String> future = service.submit(new Callable<String>(){
			@Override
			public String call() throws Exception {
				System.out.println("执行多线程");
				Thread.sleep(100);
				return "hmm";
			}});
		
		/**
		 *    业务代码
		 */
//		取消任务，如果为false，表明如果任务正在执行中则不取消
//		future.cancel(true);
		try {
			future.get(10, TimeUnit.MILLISECONDS);
		} catch (InterruptedException | ExecutionException | TimeoutException e) {
			System.out.println("执行超时");
			e.printStackTrace();
		}
	}
	
}
